package com.chixing.ih.module.task.service.impl;


import com.chixing.ih.module.programmer.mapper.ImageMapper;
import com.chixing.ih.module.programmer.mapper.ProgrammerMapper;
import com.chixing.ih.module.programmer.pojo.Programmer;
import com.chixing.ih.module.programmer.pojo.UserImage;
import com.chixing.ih.module.system.pojo.User;
import com.chixing.ih.module.task.mapper.TaskMapper;
import com.chixing.ih.module.task.pojo.Comment;
import com.chixing.ih.module.task.pojo.DTO.MongoDBCommentDTO;
import com.chixing.ih.module.task.repository.CommentRepository;
import com.chixing.ih.module.task.service.ICommentService;
import com.chixing.ih.util.EmptyUtil;
import com.chixing.ih.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 目的:
 *
 * @author ：zcw
 * @version: 1.0  2020/10/3 22:00
 */
@Service
@Transactional
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private ImageMapper imageMapper;

    @Autowired
    private ProgrammerMapper programmerMapper;

    public List<Comment> findAll() {
        List<Comment> list = commentRepository.findAll();
        return list;
    }

    public List<MongoDBCommentDTO> findByCommentParentId(Long commentParentId) {

        List<MongoDBCommentDTO> list = commentRepository.findByCommentParentId(commentParentId);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setReply(commentRepository.findByCommentParentId(list.get(i).getCommentParentId()));
        }

        return list;

//        Comment comment = commentRepository.findById(id).get();
//        return comment;
    }

    public Integer saveComment(Comment comment) {
        String id = idWorker.nextId() + "";
        comment.set_id(id);

        comment.setCreateTime(new Date());
        comment.setThumbup(0);

        commentRepository.save(comment);
        return 1;
    }

    public void updateById(Comment comment) {
        commentRepository.save(comment);
    }

    @Override
    public void deleteByCommentId(Long commentId) {
        commentRepository.deleteByCommentId(commentId);
    }


    public List<MongoDBCommentDTO> findByTaskIdAndCommentParentId(Long taskId) {
        List<MongoDBCommentDTO> list = commentRepository.findByTaskIdAndCommentParentId(taskId, 0);
        return list;
    }

    public void thumbUp(Long commentId) {
//        Comment comment = commentRepository.findByCommentId(commentId);
//
//        comment.setThumbup(comment.getThumbup() + 1);
//
//        commentRepository.save(comment);

        Query query = new Query();
        query.addCriteria(Criteria.where("commentId").is(commentId));

        Update update = new Update();
        update.inc("thumbup", 1);

        mongoTemplate.updateFirst(query, update, "comment");

    }
}
